  //index.js
//获取应用实例
const app = getApp();
// var template = require('../header/header.js');

Page({
    data: {
        sexArray: ['未选择', '男', '女'],
        index: 0,
        userInfo: {},
        codeInfo: '获取验证码',
        codeTime: 60,
        codeLock: false,
        code: '',
        phoneCache: ''
    },

    inputPhone(e){
        this.setData({
            phoneCache: e.detail.value
        })
    },

    getCode(){

        let _this = this;
        let count = 1;
        let time = null;
        
        if(this.data.codeLock){
            return
        }
        _this.setData({
            codeLock: true
        })      

        const params = {
            url: app.api.url.assistant.sendSms,
            data: {
                type: 4,
                phone: _this.data.phoneCache || _this.data.userInfo.phone,
                user_type: 2
            },
        };

       
      
        time = setInterval(() =>{

            let info = `${count} 秒`;

            if(count === this.data.codeTime){
                clearInterval(time)
                _this.setData({
                    codeLock: false,
                    codeInfo: '获取验证码'
                })

                return
            }

            count++;

            _this.setData({
                codeInfo: info
            })
            
            
        }, 1000)
        

        app.request(params).then(res =>{
            app.showToast('已经发送，请查收')
        }).catch(err =>{
            app.showToast('获取失败,请重试')
        })
    },
    
    onLoad: function () {
        
        if(!app.globalData.userInfo) {

            this.getUserInfo();
            
        } else {

            this.setData({
                userInfo: app.globalData.userInfo,
                index: app.globalData.userInfo.sex
            })

        }
    },
    onHide: function () {
    },
    
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.getUserInfo();
    },

    changeSex: function(e) {
        
        let userInfo = this.data.userInfo;
        userInfo.sex = e.detail.value;
        
        this.setData({
            userInfo
        })

    },

    submit: function(e) {
        var data = e.detail.value;
        data.sex = this.data.index;
        
        for(var i in data) {
            if(data[i] == '' || data[i] == 0) {
                app.showToast('请先完善信息再提交');
                return ;
            }
        }

        var object = {
            url: app.api.url.user.infoEdit,
            data: data,
        }

        app.request(object).then(res =>{
            
            if(res.status == 1) {
                app.navigateTo(app.page.home)
            } else {
                app.showToast(res.info);
            }
        })

        

    },

    /**
     * 获取用户信息
     */
    getUserInfo: function(){
        var _this = this;
        var object = {
            url: app.api.url.user.userInfo,
            complete: function() {
                wx.stopPullDownRefresh() //停止下拉刷新
            }
        }

        app.request(object).then(res =>{
            
            if(res.status == 1) {

                console.log('req userinfo', res)
                app.globalData.userInfo = res.data;
                
                _this.setData({
                    userInfo: res.data,
                    index: res.data.sex
                })

            } else {
                app.showToast(res.info);
            }
            
        });
    },
});
